package org.ccahouse.ccacommons.utils;

import javax.validation.constraints.NotNull;
import java.util.Random;
import java.util.UUID;

/**
 * @author kevin
 * @since 2020-07-06
 */
public class RandomGeneratorUtils {

    /**
     * 随机生成指定长度数字
     * @param length 生成的数字长度
     * @return 随机数字
     */
    public static String numbers(@NotNull final int length) {
        String seed = "1234567890";
        return generator(length, seed);
    }

    /**
     * 随机生成指定长度字母
     * @param length 字符串长度
     * @return 随机字母
     */
    public static String strings(@NotNull final int length) {
        String seed = "qwertyuiopasdfghjklzxcvbnm";
        return generator(length, seed);
    }

    private static String generator(int length, String base) {
        Random random = new Random();
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            builder.append(base.charAt(number));
        }
        return builder.toString();
    }

    public static String get32UUID() {
        String uuid = UUID.randomUUID().toString();
        uuid = uuid.replaceAll("-", "");
        return uuid;
    }

    public static String getUpper32UUID() {
        return UUID.randomUUID().toString().toUpperCase();
    }
}
